package com.baimifan.renovation;

import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.IdRes;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.baimifan.renovation.homefrag.BuildFragment;
import com.baimifan.renovation.homefrag.FindFragment;
import com.baimifan.renovation.homefrag.MyFragment;
import com.baimifan.renovation.homefrag.HomeFragment;
import com.baimifan.renovation.tools.statusBar;
import com.gyf.barlibrary.ImmersionBar;

public class HomeActivity extends FragmentActivity {
    private Activity home;
    private RadioGroup home_radio;
    private FrameLayout home_frame;
    private HomeFragment homeFragment;
    private BuildFragment buildFragment;
    private FindFragment findFragment;
    private MyFragment myFragment;
    private Float w, h, widthPixels, heightPixels;
    private View home_view;
    private LinearLayout flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        home = HomeActivity.this;
        ImmersionBar.with(home).init();
        init();
        getDisplay();
        setLayoutParams();
        setDefaultFragment();
        home_radio.setOnCheckedChangeListener(new HomeCheckListener());
    }

    private void setLayoutParams () {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)(h*49));
        home_radio.setLayoutParams(params);

        LinearLayout.LayoutParams params_view = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)(5.4));
        home_view.setLayoutParams(params_view);

        LinearLayout.LayoutParams params_flag = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBar.getStatusBarHeight(home));
        flag.setLayoutParams(params_flag);
    }

    private void getDisplay() {
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        widthPixels = Float.valueOf(metrics.widthPixels);
        heightPixels = Float.valueOf(metrics.heightPixels);
        w = widthPixels / 360;
        h = heightPixels / 640;
    }

    private void init () {
        home_radio = (RadioGroup) findViewById(R.id.home_radio);
        home_frame = (FrameLayout) findViewById(R.id.home_frame);
        home_view = findViewById(R.id.home_view);
        flag = (LinearLayout) findViewById(R.id.flag);
    }

    private void setDefaultFragment () {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        homeFragment = new HomeFragment();
        transaction.add(R.id.home_frame, homeFragment, "home");
        transaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    class HomeCheckListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

           FragmentManager fragmentManager = getSupportFragmentManager();
           FragmentTransaction transaction = fragmentManager.beginTransaction();
            switch (checkedId) {
                case R.id.home_home:
                    flag.setVisibility(View.VISIBLE);
                    if (homeFragment == null) {
                        homeFragment = new HomeFragment();
                    }
                    if (fragmentManager.findFragmentByTag("home") == null) {
                        transaction.add(R.id.home_frame, homeFragment, "home");
                    } else {
                        transaction.show(homeFragment);
                    }
                    if (buildFragment != null) {
                        transaction.hide(buildFragment);
                    }
                    if (findFragment != null) {
                        transaction.hide(findFragment);
                    }
                    if (myFragment != null) {
                        transaction.hide(myFragment);
                    }
                    break;
                case R.id.home_build:
                    flag.setVisibility(View.VISIBLE);
                    if (buildFragment == null) {
                        buildFragment = new BuildFragment();
                    }
                    if (fragmentManager.findFragmentByTag("build") == null) {
                        transaction.add(R.id.home_frame, buildFragment, "build");
                    } else {
                        transaction.show(buildFragment);
                    }

                    if (homeFragment != null) {
                        transaction.hide(homeFragment);
                    }
                    if (findFragment != null) {
                        transaction.hide(findFragment);
                    }
                    if (myFragment != null) {
                        transaction.hide(myFragment);
                    }

                    break;
                case R.id.home_find:
                    flag.setVisibility(View.VISIBLE);
                    if (findFragment == null) {
                        findFragment = new FindFragment();
                    }
                    if (fragmentManager.findFragmentByTag("find") == null) {
                        transaction.add(R.id.home_frame, findFragment, "find");
                    } else {
                        transaction.show(findFragment);
                    }
                    if (buildFragment != null) {
                        transaction.hide(buildFragment);
                    }
                    if (homeFragment != null) {
                        transaction.hide(homeFragment);
                    }
                    if (myFragment != null) {
                        transaction.hide(myFragment);
                    }
                    break;
                case R.id.home_my:
                    flag.setVisibility(View.GONE);
                    if (myFragment == null) {
                        myFragment = new MyFragment();
                    }
                    if (fragmentManager.findFragmentByTag("my") == null) {
                        transaction.add(R.id.home_frame, myFragment, "my");
                    } else {
                        transaction.show(myFragment);
                    }
                    if (buildFragment != null) {
                        transaction.hide(buildFragment);
                    }
                    if (findFragment != null) {
                        transaction.hide(findFragment);
                    }
                    if (homeFragment != null) {
                        transaction.hide(homeFragment);
                    }
                    break;
            }
            transaction.commit();
        }
    }
}
